/*
 * ============================================================================
 *
 *       Filename:  itimer.c
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2014年03月19日 15时50分20秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  jianxi sun (jianxi), ycsunjane@gmail.com
 *   Organization:  
 *
 * ============================================================================
 */
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include <sys/time.h>
#include <string.h>
#include <signal.h>
#include <errno.h>

sigset_t sigset;

void *killsignal(void *sigp)
{
	pthread_sigmask(SIG_BLOCK, &sigset, NULL);

	int i;
	for(i = 0; i < 1000; i++){
		kill(getpid(), *(int *)sigp);
	}
}

void *sigpro(void *arg)
{
	int err, signo, i = 0;
	while(1) {
		err = sigwait(&sigset, &signo);	
		if(err != 0) {
			printf("sig wait failed\n");
			exit(-1);
		}

		switch(signo) {
		case SIGPROF:
			printf("signal:%s, time:%d\n", "SIGPROF", i++);
		}
	}
}

int main()
{	
	sigemptyset(&sigset);
	sigaddset(&sigset, SIGPROF);

	pthread_sigmask(SIG_BLOCK, &sigset, NULL);

	int sig = SIGPROF;
	pthread_attr_t pid_attr;
	pthread_attr_init(&pid_attr);
	pthread_attr_setdetachstate(&pid_attr, PTHREAD_CREATE_DETACHED);

	pthread_t pid;
	pthread_create(&pid, &pid_attr, killsignal, &sig);

	pthread_t sigproc_pid;
	pthread_create(&sigproc_pid, &pid_attr, sigpro, NULL);

	while(1);
}
